<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
class BuyController extends Controller
{
    //开通会员
    public function buy()
    {
    	return view('home.vip_service.buy');
    }

    //查询价格
    public function select(Request $request)
    {
    	$data = $request -> all();

    	if($data['type'] == '白金会员')
    	{
    		$type = 0;
    	}else
    	{
    		$type = 1;
    	}

    	$res = DB::table('vip') -> where('type',$type) -> first();


    	if($res)
    	{
    		return response() -> json($res -> price);
    	}


    }

    //提交订单
    public function insert(Request $request)
    {
    	//准备数据
    	$order = [];

    	//获取用户id
    	$uid = Session::get('master') -> id;
    	$order['uid'] = $uid;
    	$data = $request -> all();

    	//会员类别
    	if($data['type'] == '超级会员')
    	{
    		$order['goods'] = $data['type'];
    		$type = 1;
    	}else
    	{
    		$order['goods'] = $data['type'];
    		$type = 0;
    	}

    	//查询单价格
    	$res = DB::table('vip') -> where('type',$type) -> first() -> price;

    	//计算金额
    	if($data['m'] == 1)
    	{
    		$account = $res * $data['m'];
    	}elseif($data['m'] == 3)
    	{
    		$account = $res * $data['m'] * 0.9;
    	}else
    	{
    		$account = $res * $data['m'] * 0.8;
    	}

    	$order['account'] = $account;
        date_default_timezone_set("PRC");
        $order['time'] = time();
        $num = date('mdHi'.time()).$uid.rand(10000,99999);
        $order['num'] = $num;
    	$r = DB::table('order') -> insert($order);

    	if($r)
    	{
    		return response() -> json($num);
    	}
    

    }


}
